using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using MimeCloud.AssetManagement.Editor.AssetCreation;
using System.Windows.Media.Imaging;
using MimeCloud.AssetManagement.AssetRendering;
using MimeCloud.AssetManagement.Business.Interface;

namespace MimeCloud.AssetManagement.DigitalLibrary.Converters
{
    public class AssetListItemConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            IAsset data = value as IAsset;
            if (data == null)
                return String.Empty;
            string param = parameter.ToString();

            switch (param.Trim().ToLowerInvariant())
            {
                case "filename":
                    if (data.IsLocalAsset())
                    {
                        return "New Asset";// (data as LocalAsset).UniqueLocalId.ToString();
                        //if (data.AssetContentList.Count>0)
                        //    return d
                    }
                    else return String.Concat("ID:", data.ID.ToString());
                    break;
                case "thumbimg":
                    //return ImageSource
                    //do we have an image?
                    foreach (IAssetContent content in data.AssetContentList)
                    {
                        if (content.AssetContentType.ContentType==TypeOfContent.Image)
                            return ThumbnailHelper.GetImageForContent(content);
                    }
                    //no image, return new asset thumb
                    return new BitmapImage(new Uri(@"pack://application:,,/Images/newasset.png"));
                    break;
                case "childdetail":
                    int imageCount =
                        data.AssetContentList.Where(c => c.AssetContentType.ContentType == TypeOfContent.Image).Count();
                    int videoCount =
                        data.AssetContentList.Where(c => c.AssetContentType.ContentType == TypeOfContent.Video).Count();
                    string result = String.Empty;
                    if (imageCount > 0)
                        result = String.Concat(imageCount, " images");
                    if (videoCount > 0)
                        result = String.Concat(result, ", ", videoCount, " videos").Trim(new char[] {','}).Trim();
                    if (String.IsNullOrEmpty(result))
                        result = "no content";
                    return result;
                    break;
            }
            return String.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
